<%-- 
    Document   : cadastraUsuario
    Created on : 07/10/2011, 08:10:15
    Author     : 0433101
--%>

<%@page import="Exceptions.PrivilegioInsuficienteException"%>
<%@page import="Model.Usuario"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@include file = "autenticacaoAdministrador.jsp"%>
<!DOCTYPE html>
<html>
    <head> 
        <title>Sistema Controle de Reservas</title>  
        <script type="text/javascript">
            var xmlhttp;//objeto de requisicao
            var file;//variavel que guardara o endereco a ser requerido
            
            function validar(form){
                var nome = form.nome.value;
                var login = form.login.value;
                var senha = form.senha.value;
                var email = form.email.value;
                var nascimento = form.nascimento.value;
                document.getElementById("nomeAlert").innerHTML = "";
                document.getElementById("loginAlert").innerHTML = "";
                document.getElementById("senhaAlert").innerHTML = "";
                document.getElementById("emailAlert").innerHTML = "";
                document.getElementById("nascimentoAlert").innerHTML = "";
                
                if(nome == ""){
                    document.getElementById("nomeAlert").innerHTML = "NOME DEVE SER PREENCHIDO!";
                    form.nome.focus();
                    return false;
                }
                else if(login == ""){
                    document.getElementById("loginAlert").innerHTML = "LOGIN DEVE SER PREENCHIDO!";
                    form.login.focus();
                    return false;
                }
                else if(nascimento != ""){
                    if(!VerificaData(nascimento)){
                        document.getElementById("nascimentoAlert").innerHTML = "DATA INVÁLIDA!";
                        form.nascimento.focus();
                        return false;
                    }
                    return true;
                }
                else if(senha == ""){
                    document.getElementById("senhaAlert").innerHTML = "SENHA DEVE SER PREENCHIDO!";
                    form.senha.focus();
                    return false;
                }
                else if(email == ""){
                    document.getElementById("emailAlert").innerHTML = "EMAIL DEVE SER PREENCHIDO!";
                    form.email.focus();                    
                    return false;
                }
                else if(!checkMail(email)){
                    document.getElementById("emailAlert").innerHTML = "EMAIL INVÁLIDO!";
                    form.email.focus();
                    return false;
                }
                else return true;
            }
            
            function checkMail(mail){
                var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
                if(typeof(mail) == "string"){
                    if(er.test(mail))
                    { 
                        return true; 
                    }
                    return false;
                }else if(typeof(mail) == "object"){
                    if(er.test(mail.value)){ 
                        return true; 
                    }
                    return false;
                }else{
                    return false;
                }
            }
            
            function VerificaData(digData) 
            {
                var bissexto = 0;
                var data = digData; 
                var tam = data.length;
                if (tam == 10) 
                {
                        var dia = data.substr(0,2)
                        var mes = data.substr(3,2)
                        var ano = data.substr(6,4)
                        if ((ano > 1900)||(ano < 2100))
                        {
                                switch (mes) 
                                {
                                        case '01':
                                        case '03':
                                        case '05':
                                        case '07':
                                        case '08':
                                        case '10':
                                        case '12':
                                                if  (dia <= 31) 
                                                {
                                                        return true;
                                                }
                                                break

                                        case '04':              
                                        case '06':
                                        case '09':
                                        case '11':
                                                if  (dia <= 30) 
                                                {
                                                        return true;
                                                }
                                                break
                                        case '02':
                                                /* Validando ano Bissexto / fevereiro / dia */ 
                                                if ((ano % 4 == 0) || (ano % 100 == 0) || (ano % 400 == 0)) 
                                                { 
                                                        bissexto = 1; 
                                                } 
                                                if ((bissexto == 1) && (dia <= 29)) 
                                                { 
                                                        return true;                             
                                                } 
                                                if ((bissexto != 1) && (dia <= 28)) 
                                                { 
                                                        return true; 
                                                }                       
                                                break                                           
                                    }
                            }
                    }
                    return false;
                }
                
                function IniciaAjax(){
                    var HTTP_REQUEST;

                    try{
                        HTTP_REQUEST = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch(e){
                        try{
                            HTTP_REQUEST = new ActiveXObject("Msxml2.XMLHTTP");
                        }
                        catch(ex){
                            try{
                                 HTTP_REQUEST = new XMLHttpRequest();
                                 HTTP_REQUEST.overrideMimeType('text/html');
                            }
                            catch(exc)
                            {
                                 alert("Esse browser não tem recursos para uso do Ajax");
                                 HTTP_REQUEST = null;
                            }
                        }
                    }
                    return HTTP_REQUEST;
                }
                
                function validaLogin(data){
                    document.getElementById("loginAlert").innerHTML = "";
                    var login = data.value;
                    xmlhttp = IniciaAjax();
                    if(login != ""){
                        file = "MainAccessServlet?cmd=validaCadastro&login=" + login;
                        xmlhttp.open("POST",file, true);
                        xmlhttp.send();
                        xmlhttp.onreadystatechange = response;
                    }
                }
                
                function response(){
                    if (xmlhttp.readyState==4 && xmlhttp.status==200){
                        document.getElementById("loginAlert").innerHTML = xmlhttp.responseText;
                    }
                }
        </script>
    </head>
    <body>
        <h1> Cadastro de Usuários </h1>
        <div id="message">${erro}</div>
        <form name="formCadastro" method="post" action="MainAccessServlet" onsubmit="return validar(this)" accept-charset="iso-8859-1,utf-8">
            Nome: <input type="text" name="nome"/><br>
            <label id="nomeAlert"></label><br>
            Login: <input type="text" name="login" onblur="validaLogin(this)"/><br>
            <label id="loginAlert"></label><br>
            Privilégio:
            <select name="privilegio">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
            </select><br><br>
            Data de Nascimento: <input type="text" name="nascimento"/> (dd/mm/yyyy)<br>
            <label id="nascimentoAlert"></label><br>
            Senha: <input type="password" name="senha"/><br>
            <label id="senhaAlert"></label><br>
            E-mail: <input type="text" name="email"/><br>
            <label id="emailAlert"></label><br>
            <input type="submit" value="Cadastrar"/><input type="reset" value="Limpar"/>
            <button type="button" onclick="location.href='menuAdministrador.jsp'">Cancelar</button>
            <input type="hidden" value="cadastraUsuario" name="cmd"/>
        </form>
     </body>
</html>